﻿using AutoMapper;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using NetTaste;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System.Data;
using System.Drawing.Printing;
using System.Linq.Expressions;
using System.Numerics;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using VM.Common.DataUntil;
using VM.Common.Result;
using VM.DAL;
using VM.Entity.EntityDto.common;
using VM.Entity.EntityDto.Topic;
using VM.Entity.EntityMap.Condition;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.Menu;
using VM.Entity.EntityMap.MsgCode;
using VM.Entity.EntityMap.Region;
using VM.Entity.EntityMap.Token;
using VM.Entity.EntityMap.Topic;
using VM.Entity.EntityMap.User;
using VM.IBLL.Files;
using VM.IBLL.Member;
using VM.IBLL.Token;
using VM.IBLL.Topic;
using VM.IDAL;
using VM.IDAL.Topic;
using static Dm.net.buffer.ByteArrayBuffer;

namespace VM.BLL.Topic
{
    public class TopicBLL : BaseBLL, ITopicBLL
    {

        private readonly ISqlSuggarDAL _ISqlSuggarDAL;
   
        private readonly ITopicDAL _ITopicDAL;
        private readonly IFilesBLL _IFilesBLL;
        private readonly IMapper _IMapper;

        public TopicBLL(ISqlSugarClient client, ISqlSuggarDAL isqlSuggarDAL,   ITopicDAL iTopicDAL, IFilesBLL iFilesBLL, IMapper iMapper)
        {
            _ISqlSuggarDAL = isqlSuggarDAL;
            _ITopicDAL = iTopicDAL;
            _IFilesBLL = iFilesBLL;
            _IMapper = iMapper;

        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="MemberId"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public Task<bool> updateMemberNameById(int MemberId, sys_member obj)
        {

            Task<bool> b = _ISqlSuggarDAL.UpdateAsync(obj);
            return b;


        }

        public ApiResult addTopicType(addtopicTypeDto obj)
        {
            ApiResult res = new ApiResult();
            sys_topicType topicType = _IMapper.Map<addtopicTypeDto, sys_topicType>(obj);
            bool  InsertRes = _ITopicDAL.insertTopicType(topicType);

            if (InsertRes)
            {
                res.code = 200;
                res.msg = "添加成功";
              
                 
            }
            else
            {
                res.code = 403;
                res.msg = "添加失败";
               


            }
            return res;
        }

        public ApiDataResult<PagingData<getTopicTypeListDto>> getTopicTypeList(int pageSize, int pageIndex)
        {
            ApiDataResult<PagingData<getTopicTypeListDto>> res = new ApiDataResult<PagingData<getTopicTypeListDto>>();
            List < Condition > funcWhere = null;
            Expression<Func<sys_topicType, object>> funcOrderby = (x => x.id);
            PagingData<getTopicTypeListDto> data =  _ITopicDAL.getTopicTypeListConditon(funcWhere, pageSize, pageIndex, funcOrderby,false);
            if (data.recordCount > 0)
            {

                res.code = 200;
                res.msg = "获取成功";
                res.data = data;

            }
            else
            {
                res.code = 200;
                res.msg = "无数据";
                res.data = null;
            }
            return res;

        }

        public ApiResult addTopic(sys_topic obj)
        {

            ApiResult res = new ApiResult();
            bool InsertRes = _ITopicDAL.InsertTopic(obj);
            if (InsertRes)
            {
                res.code = 200;
                res.msg = "添加成功";

            }
            else {
                res.code = 403;
                res.msg = "添加失败";

            }
            return res;
        }

        public ApiDataResult<PagingData<sys_topic>> getTopicList(int? TopictypeId, string? topicName,int pageSize, int pageIndex)
        {
            ApiDataResult<PagingData<sys_topic>> res = new ApiDataResult<PagingData<sys_topic>>();
             
            List<Condition> list = new List<Condition>();
            if (!TopictypeId.Equals(null)&& TopictypeId!=0)
            {
                Condition c = new Condition()
                {
                    FieldName = "parentId",
                    FieldValue = TopictypeId.ToString(),
                    ConditionalType = 0
                };
                list.Add(c);
            }
            if (!string.IsNullOrEmpty(topicName))
            {
                Condition c1 = new Condition()
                {
                    FieldName = "name",
                    FieldValue = topicName.ToString(),
                    ConditionalType =1
                };
                list.Add(c1);
            } 
            Expression<Func<sys_topic, object>> funcOrderby = x => x.useQty;

            PagingData<sys_topic> data = _ISqlSuggarDAL.QueryPageConditon<sys_topic>(list, pageSize, pageIndex, funcOrderby, false);

            if (data.recordCount > 0)
            {
                res.code = 200;
                res.msg = "获取成功";
                res.data = data;
            }
            else
            {
                res.code = 200;
                res.msg = "无数据";
                res.data = null;
            }
            return res;
        }
    }




}
